http = require'socket.http'

function random_word(w_type)
	--print("inside random word")
	--print("w_type : ", w_type)

	local nar_str = ""

	local nf = "txt/nouns.txt"
	local vf = "txt/verbs.txt"
	local ajf = "txt/adj.txt"
	local avf = "txt/adv.txt"

	local nouns = {}
	local verbs = {}
	local adjs = {}
	local advs = {}

	if w_type == "noun" then

		for line in love.filesystem.lines(nf) do

			table.insert(nouns, line)

		end

		return select_words(nouns)

	end

	if w_type == "verb" then

		for line in love.filesystem.lines(vf) do

			table.insert(verbs, line)

		end

		return select_words(verbs)

	end

	if w_type == "adj" then



		for line in love.filesystem.lines(ajf) do

			table.insert(adjs, line)

		end

		return select_words(adjs)

	end

	if w_type == "adv" then

		for line in love.filesystem.lines(avf) do

			table.insert(advs, line)

		end

		return select_words(advs)

	end



end

function select_words(list)

	local r = RNG:random(#list)

	return list[r]


end

function adv_adj_noun_string()

	local strs = {}
	strs[1] = random_word("adv")
	strs[1] = random_word("adj")
	strs[2] = random_word("noun")

	local ns = searchable_string(strs)

	return google_image(ns)


end

function adj_noun_string(meta_tiles)

	--print("in adj_noun_string")
	local strs = {}
	--strs[1] = random_word("adv")
	strs[1] = random_word("adj")
	strs[2] = random_word("noun")

	local ns = searchable_string(strs)

	print("ns : ", ns)

	return google_image(ns, meta_tiles)


end

function draw_narrative(meta_tiles)




	if show_nar_image == true then
		--print("showing nar image")
		love.graphics.draw(g_nar_image, 10, 10, 0,
			16/g_nar_image:getWidth(),
			16/g_nar_image:getHeight())

	end

	if meta_tiles ~= nil then

		for i = 1, #meta_tiles do


			if meta_tiles[i].image ~= nil then

				draw_in_fov(meta_tiles[i])
				--[[
				love.graphics.draw(meta_tiles[i].image,
					meta_tiles[i].x,
					meta_tiles[i].y)
					]]

			else
				love.graphics.setColor(0,0,0,0)
				love.graphics.rectangle("fill", meta_tiles[i].x, meta_tiles[i].y,
					tile_size, tile_size)
				love.graphics.setColor(255,255,255,255)

			end

			if dbg_gis_img == true then
				if meta_tiles[i].o_image ~= nil then
					print(meta_tiles[i].o_image)
					love.graphics.draw(meta_tiles[i].o_image,
						(grid_size*tile_size)/#meta_tiles+i,
						(grid_size*tile_size)/#meta_tiles, 0,
						((grid_size*tile_size)/#meta_tiles+i)/meta_tiles[i].o_image:getWidth(),
						((grid_size*tile_size)/#meta_tiles)/meta_tiles[i].o_image:getHeight()  )
				else

				end

			end

		end
	end

end

function spawn_meta_tiles(map, meta_tiles)
	print("spawning meta tiles")

	if map == "town" then

		local coords = {{x = 14, y = 31},{x = 14, y = 29},{x = 14, y = 27},
						{x = 17, y = 31}, {x = 17, y = 29}, {x = 17, y = 27}
		}


		for i = 1, #coords do
			thread_buffer[i] = love.thread.newThread(i.."",'meta_tile_thread.lua')
			thread_buffer[i]:set('x', coords[i].x)
			thread_buffer[i]:set('y', coords[i].y)
			thread_buffer[i]:set('visible', true)
			thread_buffer[i]:set('type', "meta")
			thread_buffer[i]:set('meta_id', i)

			--thread_queue(thread_buffer[i])


		end

	end

end

function update_meta_tiles(meta_tiles, thread)
	print("in update meta tiles")


	if thread:get('done') == false then
		thread:wait()
	end

	MetaTile = { x = thread:get('x')*tile_size, y = thread:get('y')*tile_size,
		visible = thread:get('visible'), type = thread:get('type'),
		meta_id = thread:get('meta_id'), image = thread:get('image'),
		name = thread:get('name'), --[[o_image = thread:get('orig_img')]]}


	if MetaTile.image ~= nil then
		print("MetaTile Image W: ", MetaTile.image:getWidth(), " H : ", MetaTile.image:getHeight(),
			"type :", MetaTile.image:type())
	end

	table.insert(meta_tiles, MetaTile)

		--thread[i]:set('final', true)

	threads_complete = threads_complete +1


	print(table.tostring(meta_tiles))

end

function searchable_string(strs)
	--print("in searchable string")

	local newstring = ""

	for i = 1, #strs do

		if newstring ~= "" then
			newstring = newstring.."+"..strs[i]
		else
			newstring = strs[i]
		end

	end
	--print("new string: ", newstring)

	return newstring


end

function google_string(str)

	local url = "http://google.com/search?q="..str
	local search_results = http.request(url)



end


function google_image(str, meta_tiles)
	print ("in google_image")
	meta_tile_count = meta_tile_count + 1

	local url = "http://google.com/search?q="..str.."&tbm=isch" -- &num=1 for first result
	local search_results = http.request(url)
	local reject_images = {
		"/images/google_favicon_128.png",
		"/images/nav_logo124.png",

	}

	local file_types = {
		"png",
		"jpg",
		"gif",
		"bmp",

	}
	--search_results = search_results:gsub("<[%S>]+",string.upper)
	--[%w_-]-%.png
	local images = {}

	for i = 1, #file_types do
		search_results, result_num = search_results:gsub("[%w_%-:/%.%%%$]+%."..file_types[i], function (s)

			for j = 1, #reject_images do
				if reject_images[j] == s then s = "done" end
			end

			if s:sub(1,1) ~= "h" then
				s = "done"
			end

			if s ~= "done" then


				if this_thread:getName() ~= nil then
					print(this_thread:getName(), "accepted s : ", s)
				else
					print("accepted s : ", s)
				end

				table.insert(images, s)
				table.insert(reject_images, s)

			end

		end)
	end

	result_num = #images

	if this_thread:getName() ~= nil then
		print(this_thread:getName(), "url:  ", url, "results : ", result_num --[[, "text: ", table.tostring(images)]])
	else
		print("url:  ", url, "results : ", result_num --[[, "text: ", table.tostring(images)]])
	end


	local picked_image = images[RNG:random(#images)]

	meta_tiles[meta_tile_count] = {}

	if this_thread:getName() ~= nil then
		print(this_thread:getName(), "requesting...", picked_image)
	else
		print("requesting.. ", picked_image)
	end

	picked_image:gsub("[%w_%-]+%.[%w]+", function(s)
		s = string.sub(s, 1, -5)
		s = s:gsub("[%d_%-]+", " ")
		s = s:gsub("[%w]+", string.lower)
		s = s:gsub("^[%s]+", "")
		s = s:gsub("[%s][%w][%s]", "")

		s = s:sub(1,64)


		if this_thread:getName() ~= nil then
			print(this_thread:getName(), "s : ", s)
		else
			print("s : ", s)
		end

		meta_name = s
		temp_name = s
		return s
	end)

	--print("image_name :", image_name)

	if this_thread:getName() ~= nil then
		print(this_thread:getName(), "meta_name : ", meta_name)
	else
		print("meta_name : ", meta_name)
	end





	table.insert(meta_tiles[meta_tile_count], meta_name)
	print("meta_tiles : ", table.tostring(meta_tiles[meta_tile_count]))


	local image_step_one = http.request(picked_image)

	if this_thread:getName() ~= nil then
		--print(this_thread:getName(), "received : ", image_step_one)
		this_thread:set('orig_img', image_step_one)
	else
		--print("received : ", image_step_one)
	end

	local i_c = image_step_one:sub(1, 1)
	--i_c = i_c:gsub("^[%s]+", "")

	if this_thread:getName() ~= nil then
		print(this_thread:getName(), "i c : ", i_c)
	else
		print("i c : ", i_c)
	end

	if i_c ~= "<" then
		local image_step_two = love.filesystem.newFileData(image_step_one, '', 'file')
		if this_thread:getName() ~= nil then
			print(this_thread:getName(), "data type : ", image_step_two:type())
		else
			print("data type : ", image_step_two:type())
		end

		local image_step_three = love.image.newImageData(image_step_two)
		local image_step_four = love.graphics.newImage(image_step_three)
		local image_booya = image_step_four



		return image_step_four
	else

		if this_thread:getName() ~= nil then
			print(this_thread:getName(), "invalid i_c")
		else
			print("invalid i_c")
		end

		return green_img
	end



end


